package com.tanhua.server.controller;

import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.*;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingService;
import com.tanhua.server.service.TanhuaService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: Liking
 * @description
 * @date: 2023/7/24 19:16
 */
@RestController
@RequestMapping("/users")
@Slf4j
public class UsersController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private SettingService settingService;

    @Autowired
    private UserService userService;

    @Autowired
    private TanhuaService tanhuaService;


    /**
     * 查询用户资料
     */
    @GetMapping
    public ResponseEntity users(Long userID) {
        if(userID == null) {
            userID = UserHolder.getUserId();
        }
        UserInfoVo userInfo = userInfoService.findById(userID);
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 更新用户资料
     */
    @PutMapping
    public ResponseEntity updateUserInfo(@RequestBody UserInfo userInfo) {
        userInfo.setId(UserHolder.getUserId());
        userInfoService.update(userInfo);
        return ResponseEntity.ok(null);
    }


    @PostMapping("/header")
    public ResponseEntity updateHeader(MultipartFile headPhoto) {
        //3、调用service
        try {
            userInfoService.updateHead(headPhoto,UserHolder.getUserId());
        } catch (BusinessException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("请上传带有人脸的照片");
        } catch (IOException e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("未知错误");
        }
        return ResponseEntity.ok(null);
    }

    /**
     * 用户通用设置 - 读取
     */
    @GetMapping("/settings")
    public ResponseEntity<SettingsVo> settings() {
        SettingsVo vo = settingService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * 设置陌生人问题
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map) {
        //获取参数
        String content = (String) map.get("content");
        settingService.saveQuestion(content);
        return ResponseEntity.ok(null);
    }

    /**
     * 通知设置
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity notifications(@RequestBody Map map) {
        //获取参数
        settingService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 分页查询黑名单列表
     */
    @GetMapping("/blacklist")
    public ResponseEntity blacklist(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        //1、调用service查询
        PageResult pr = settingService.blacklist(page,size);
        //2、构造返回
        return ResponseEntity.ok(pr);
    }

    /**
     * 取消黑名单
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackUserId) {
        settingService.deleteBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 手机端修改手机号码
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        //1、获取参数
        String phone = UserHolder.getMobile();
        ErrorResult errorResult = null;
        try {
        errorResult = userService.sendMsg(phone);
        if (null == errorResult) {
            return ResponseEntity.ok(null);
        }
        } catch (BusinessException e) {
            log.error("发送短信验证码失败~ phone = " + phone, e);
            errorResult = ErrorResult.fail();
        } catch (Exception e) {
            errorResult = ErrorResult.error();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResult);
    }

    /**
     * 校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String,String> verificationCode) {
        //1、获取参数
        String phone = UserHolder.getMobile();
        //2、调用service
        Boolean verification = userService.checkVerificationCode(phone, verificationCode.get("verificationCode"));
        //3.构造返回
        Map<String,Boolean> map = new HashMap<>();
        map.put("verification", verification);
        return ResponseEntity.ok(map);
    }

    /**
     * 保存新手机号码
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody User user) {
        //1、获取参数
        Long userId = UserHolder.getUserId();
        user.setId(userId);
        //2、调用service
        userService.updatePhone(user);
        return ResponseEntity.ok(null);
    }

    /**
     * 用户喜欢
     */
    @PostMapping("/fans/{uid}")
    public ResponseEntity fans(@PathVariable("uid") Long likeUserId) {
        tanhuaService.likeUser(likeUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * 统计
     */
    @GetMapping("/counts")
    public CountsVo counts() {
        return tanhuaService.counts();
    }

    @GetMapping("/friends/{type}")
    public ResponseEntity friends(@PathVariable("type") int type,
                                  @RequestParam(defaultValue = "1") int page,
                                  @RequestParam(defaultValue = "10") int pagesize,
                                  String nickname) {
        UserInfo userInfo = new UserInfo();
        if(nickname != null){
            userInfo.setNickname(nickname);
        }
        PageResult pageResult = tanhuaService.friends(type, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    @DeleteMapping("/like/{uid}")
    public ResponseEntity deleteLike(@PathVariable("uid") Long likeUserId) {
        tanhuaService.notLikeUser(likeUserId);
        return ResponseEntity.ok(null);
    }

}
